<template>
  <div ref="imageWrapper">
    <a-card title="查询条件" class="search">
      <div class="table-page-search-wrapper">
        <a-form-model layout="inline" ref="ruleForm" :model="queryParam" :rules="rules" @keyup.enter.native="onSearch">
          <a-row :gutter="24">
            <a-col :md="6" :sm="8">
              <a-form-model-item label="类型" prop="insidePro">
                <j-dict-select-tag
                  v-model="queryParam.insidePro"
                  placeholder="请选择"
                  dictCode="inside_pro"
                  style="width: 100%"
                />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-model-item label="年度" prop="yearNum">
                <a-date-picker
                  mode="year"
                  placeholder="请选择年份"
                  format="YYYY"
                  value-format="yyyy-MM-dd"
                  v-model="queryParam.yearNum"
                  :open="yearPickShow"
                  style="width: 100%"
                  @panelChange="handlePanelChange"
                  @openChange="handleOpenChange"
                />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="8" :offset="6">
              <span style="float: right; overflow: hidden" class="table-page-search-submitButtons">
                <a-button type="primary" @click="onSearch" icon="search" class="yellow_search_btn">查询</a-button>
                <a-button @click="searchReset" icon="reload" class="clear_btn">清除条件</a-button>
                <a-button
                  type="danger"
                  :loading="ydLoading"
                  @click="handleToYD(false)"
                  icon="arrow-right"
                  style="margin-left: 18px"
                  >转疑点</a-button
                >
                <a-button v-if="isBackButton" type="primary" style="margin-left: 8px" @click="backUppage">
                  返回
                </a-button>
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
    </a-card>
    <a-card style="margin-top: 20px" v-if="dataSource.length != 0">
      <a-tabs
        default-active-key="0"
        @change="handleRequst"
        v-model="activeKey"
        type="card"
        class="tabs_style"
        style="margin-top: 5px"
      >
        <a-tab-pane key="0" tab="工程项目资产质量财务端">
          <div style="height: 780px; overflow: visible">
            <QualityAdjustmentChart></QualityAdjustmentChart>
          </div>
        </a-tab-pane>
        <a-tab-pane key="1" tab="工程项目资产质量财务端">
          <a-table
            ref="table"
            size="middle"
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="ipagination"
            :loading="loading"
            @change="handleTableChange"
            :row-selection="rowSelection"
            class="antableClass"
            :scroll="{ x: 1800 }"
          >
            <template slot="money" slot-scope="text, record">
              <a @click="ViewInfor(queryParam, '', detailUrl)">{{ text }}</a>
            </template>
            <template slot="sameLevel" slot-scope="text, record">
              <a @click="ViewInfor(queryParam, '1', detailUrl)">{{ text }}</a>
            </template>
            <template slot="groupCompany" slot-scope="text, record">
              <a @click="ViewInfor(queryParam, '2', detailUrl)">{{ text }}</a>
            </template>
            <template v-for="(item, index) in items" :slot="item.text" slot-scope="text">
              <a :key="index" @click="ViewInfor(queryParam, item.value, detailUrl)">{{ text }}</a>
            </template>
          </a-table>
        </a-tab-pane>
      </a-tabs>
    </a-card>
    <YDModel ref="YDModel" :routerData="$route.path" />
    <ProjectQualityDetails ref="ProjectQualityDetails" :columns="columns2" />
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { ModelMixins } from '../../mixins/ModelMixins'
import columns from './columns'
import { columns2 } from '../components/columnsDetails'
import { base64toImg } from '@/mixins/base64toImg'
import YDModel from '../rqzbkh/YDModel'
import html2canvas from 'html2canvas'
import ProjectQualityDetails from '../components/ProjectQualityDetails.vue'
import QualityAdjustmentChart from './QualityAdjustmentChart.vue'
import jumpConfigMixin from '@/views/sj/sjxmzygl/sjxmss/jumpConfigMixin'
import { httpAction, getAction, postAction, deleteAction, putAction } from '@/api/manage'
import { currentY } from '@/mixins/commontime.js'
export default {
  mixins: [JeecgListMixin, base64toImg, ModelMixins, jumpConfigMixin],
  components: { YDModel, ProjectQualityDetails, QualityAdjustmentChart },
  watch: {
    // 转疑点跳转回来解析
    $route: {
      handler: function(to) {
        if (Object.keys(to.query).length) {
          this.disableMixinCreated = true
          this.selectedRowKeys = to.query.selectedRowKeys.split(' ') || []
          to.query.selectedRowKeys && delete to.query.selectedRowKeys
          this.activeKey = to.query.activeKey || '0'
          to.query.activeKey && delete to.query.activeKey
          console.log(to.query)
          this.queryParam = Object.assign(this.queryParam, to.query)
          // this.url.list = this.list[Number.parseInt(this.activeKey)]
          this.$nextTick(() => {
            this.onSearch()
          })
        } else if (Object.keys(to.params).length && to.params.from == 'sjxmss') {
          let { endDate, startDate } = JSON.parse(to.params.returnRecord)
          this.queryParam.yearNum = startDate && startDate.split('-')[0]
          this.$nextTick(() => {
            // 这可能存在问题
            this.onSearch()
          })
          this.isBackButton = true
          this.returnRecord = to.params.returnRecord
        }
      },
      immediate: true
    }
  },
  computed: {
    // 行选择
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: (selectedRowKeys, selectedRows) => {
          this.selectedRowKeys = [...selectedRowKeys]
          this.selectedRows = [...selectedRows]
        }
      }
    }
  },
  data() {
    return {
      disableMixinCreated: true,
      columns: columns(this),
      dataSource: [],
      queryParam: {
        modelId: '67e29f69f20b43ace3e578edbb6bccf1',
        insidePro: '',
        yearNum: currentY()
      },
      isBackButton: false,
      url: {
        list: '/sjmx/queryGcxmzczlcwd',
        // 查询模型转疑点信息(多选)
        TurnSuspects: '/sjmx/queryModelMsgMulti',
      },
      detailUrl: '/sjmx/queryGcxmzczlcwdXq',
      layout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 18 }
      },
      rules: {
        // insidePro: [{ required: true, message: '请选择类型', trigger: 'change' }],
        yearNum: [{ required: true, message: '请选择年度', trigger: 'change' }]
      },
      items: [
        { text: 'ztjt', value: 1 },
        { text: 'ztnf', value: 2 },
        { text: 'zttz', value: 3 },
        { text: 'ztkt', value: 4 },
        { text: 'ztct', value: 5 },
        { text: 'ztst', value: 6 },
        { text: 'ztbf', value: 7 },
        { text: 'ztwl', value: 8 },
        { text: 'ztgtz', value: 9 },
        { text: 'ztfzt', value: 10 },
        { text: 'ztgzt', value: 11 }
      ],
      columns2: columns2(this),
      activeKey: '0',
      selectedRows: [],
      selectedRowKeys: []
    }
  },
  methods: {
    searchReset() {
      this.dataSource = []
      this.queryParam = {
        modelId: '67e29f69f20b43ace3e578edbb6bccf1',
        insidePro: '',
        yearNum: ''
      }
    },
    onSearch() {
      this.$refs.ruleForm.validate(value => {
        if (value) {
          this.loadData(1)
        }
      })
    },
    handleToYD(record) {
      if (this.dataSource.length || this.EchartsSource.length) {
        debugger
        const { startTime = '', endTime = '' } = this.queryParam
        let ydnr = undefined
        let gzzd = undefined

        let time = `【工程项目资产质量财务端】`
        console.log('this.activeKey------>', this.activeKey)
        let param = Object.assign(
          {},
          this.queryParam,
          { activeKey: this.activeKey },
          { selectedRowKeys: this.selectedRowKeys.join(' ') }
        )
        let ydList = this.getselectRowJosn(this.selectedRows)
        postAction(this.url.TurnSuspects, Object.assign({}, this.queryParam, { ydList: ydList })).then(res => {
          if (res.success) {
            if (res.result.length) {
              ydnr = res.result[0].ydms ? res.result[0].ydms : ydnr
              gzzd = res.result[0].gzzd ? res.result[0].gzzd : gzzd
            }
            this.addYd(param, ydnr, time, gzzd)
          }
          if (res.code === 510) {
            this.$message.warning(res.message)
          }
        })
      } else {
        this.$message.error('空数据不能新建疑点')
      }
    },
    addYd(record, ydnr, time, gzzd) {
      this.ydLoading = true
      html2canvas(this.$refs.imageWrapper).then(canvas => {
        let dataURL = canvas.toDataURL('image/png')
        console.log('父组件调用完了')
        this.$refs.YDModel.show(record, dataURL, ydnr, time, gzzd)
        this.ydLoading = false
      })
    },
    getselectRowJosn(selectedRows) {
      selectedRows.map(item => {
        item.zb = item.target
      })
      return JSON.stringify(selectedRows)
    },
    ViewInfor(param, type, detailUrl) {
      this.$refs.ProjectQualityDetails.edit(param, type, detailUrl)
    },
    handleRequst(key) {
      this.activeKey = key
      // this.url.list = this.list[Number.parseInt(key)]
      console.log('activeKey---->', this.activeKey)
      this.onSearch()
    }
  }
}
</script>

<style lang="less" scoped>
@import '~@assets/less/tableTheme.less';
</style>
